/*
* 可增加的输入框
* */

import 'package:center_beijing/common/styles.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddCustomInputWidget extends StatefulWidget {
  final String title;
  final String placeHold;
  final Function textChange;
  final Function obscureChange;
  final bool eyeAble;
  final bool eye;
  final bool clearAll;
  final TextInputType keyboardType;
  final bool require;

  final int maxNumber;
  final List<TextInputFormatter> inputformat;
  final bool readOnly;
  final int inputNumber;
  final bool isAdd;
  final double titleFont;

  AddCustomInputWidget(
      {this.clearAll = false,
      this.inputNumber = 1,
      this.keyboardType = TextInputType.text,
      this.maxNumber = 0,
        this.titleFont,
      this.title,
        this.isAdd = true,
      this.readOnly = false,

      this.placeHold,
      this.textChange,
      this.eyeAble = false,
      this.eye = false,
      this.obscureChange,
      this.inputformat,
      this.require = false});

  @override
  _AddCustomInputWidgetState createState() => _AddCustomInputWidgetState();
}

class _AddCustomInputWidgetState extends State<AddCustomInputWidget>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  List<TextEditingController> controllerList = [];

  @override
  void initState() {
    _controller = AnimationController(vsync: this);
    super.initState();
    for (var i = 0; i < widget.inputNumber; i++) {
      controllerList.add(TextEditingController());
    }
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Theme.of(context).cardColor,
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          widget.title != null
              ? Container(
            height: Styles.MainButtonHeight,
                  child: Padding(
                    padding: EdgeInsets.fromLTRB(0, 0, Styles.spacing8, 0),
                    child: Row(
                      children: <Widget>[
                        widget.require
                            ? Text(
                                '*',
                                style: TextStyle(color: Colors.red),
                              )
                            : Container(),
                        Text(widget.title,
                            style: TextStyle(
                                fontSize: widget.titleFont!=null?widget.titleFont:Styles.MiddleFont17,
                                color: Styles.DetailColor))
                      ],
                    ),
                  ),
                )
              : Container(),
          Expanded(
              child: Column(
            children: this.controllerList.asMap().keys.map((index) {
              return Container(
                padding: EdgeInsets.fromLTRB(
                    Styles.spacing8, 0, Styles.spacing16, 0),
                child: TextField(
                  style: TextStyle(fontSize: Styles.MiddleFont17),
                  readOnly: widget.readOnly,
                  textAlign:
                      widget.title != null ? TextAlign.right : TextAlign.left,
                  controller: this.controllerList[index],
                  obscureText: widget.eye,
                  keyboardType: widget.keyboardType,
                  inputFormatters: widget.inputformat,
                  decoration: InputDecoration(
                    hintStyle: TextStyle(fontSize: Styles.MiddleFont17),
                    hintText: widget.placeHold,
                    border: InputBorder.none,
                  ),
                  onChanged: (value) {
                    if (widget.textChange != null) {
                      widget.textChange(this.controllerList);
                    }
                  },
                ),
              );
            }).toList(),
          )),
          widget.isAdd?GestureDetector(
            onTap: () {
              this.setState(() {
                this.controllerList.add(TextEditingController());
              });
            },
            child: Container(
              height: Styles.MainButtonHeight,
              margin:
                  EdgeInsets.fromLTRB(Styles.spacing8, 0, Styles.spacing8, 0),
              child: Image.asset(
                'assets/images/icon_add.png',
                width: 20.w,
                height: 20.h,
              ),
            ),
          ):Container()
        ],
      ),
    );
  }
}
